import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'


// 登录页和容器页
import LoginPage from "@/components/LoginPage"
import MainContainer from "@/components/MainContainer"

// 欢迎页
import WelcomeView from "@/views/WelcomeView"


// 系统管理
import UserManagement from "@/views/SystemManagementViews/UserManagement"
import PermissionManagement from "@/views/SystemManagementViews/PermissionManagement"
import RoleManagement from "@/views/SystemManagementViews/RoleManagement"
import OperationLog from "@/views/SystemManagementViews/OperationLog"

// 学生数据信息
import StudentData from "@/views/StudentDataView"

// 学院招生信息
import CollegeEnrollment from "@/views/CollegeEnrollmentView"

// 关于我们
import AboutUs from "@/views/AboutUsView";

// 招生省份
import EnrollmentProvince from "@/views/EnrollmentProvinceView"

// 生源地图
import StudentMap from "@/views/StudentMapView"




// 路由配置
const router = createRouter({
   history: createWebHashHistory(),
   routes: [
      {
         path: '/login',
         component: LoginPage
      },
      {
         path: '/',
         component: MainContainer,
         children: [
            {
               name: "欢迎页",
               // children 中 path 为空字符串的路由，是默认打开的路由
               path: "",
               component: WelcomeView
            },
            {
               name: "系统管理",
               path: "",
               children: [
                  {
                     name: "用户列表",
                     path: "/userManagement",
                     component: UserManagement
                  },
                  {
                     name: "权限管理",
                     path: "/permissionManagement",
                     component: PermissionManagement
                  },
                  {
                     name: "角色管理",
                     path: "/roleManagement",
                     component: RoleManagement
                  },
                  {
                     name: "操作日志",
                     path: "/operationLog",
                     component: OperationLog
                  }
               ]
            },
            {
               name: "学生数据信息",
               path: "/studentData",
               component: StudentData
            },
            {
               name: "学院招生信息",
               path: "/collegeEnrollment",
               component: CollegeEnrollment
            },
            {
               name: "招生省份信息",
               path: "/enrollmentProvince",
               component: EnrollmentProvince
            },
            {
               name: "生源地图",
               path: "/studentMap",
               component: StudentMap
            },
            {
               name: "关于我们",
               path: "/aboutUs",
               component: AboutUs
            }
         ]
      }
   ]
})


router.beforeEach((to, from, next) => {

   // 如果未匹配到路由
   // 防止搜索栏乱输入其他没有的页面路径
   if (to.matched.length === 0) {
      return next(from.path);
   }

   // 登录校验优化，除登录页面外，进入其他页面需要验证localStorage中是否有token，没有则跳转登录页面
   // 此处可以避免切换页面时，请求发出到响应“用户未登录”的时间间隔内，页面不会先刷新出来
   // 同时，路由切换到一个不会发送请求的页面时，可以实现登录校验功能
   // 故登录校验一共有两处，一处在此，路由切换校验，但可能存在服务端token过期的情况
   // 第二处位于接口请求出，每次请求如果localStorage中有token，则设置请求头，让服务端进行登录校验
   if (to.path !== "/login" && !localStorage.getItem("token")) {
      ElMessage({
         type: "warning",
         message: "请先登录",
         duration: 2000
      });
      return next("/login");
   }

   return next();
})


export default router